package com.reggie.setmeal.service.impl;

import java.util.List;
import java.util.UUID;

import com.reggie.setmeal.domain.SetmealDish;
import com.reggie.setmeal.mapper.SetmealDishMapper;
import com.reggie.setmeal.service.ISetmealDishService;
import com.reggie.setmeal.uuid.SnowflakeIdUtils;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.CategoryService;
import com.ruoyi.system.api.domain.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.reggie.setmeal.mapper.SetmealMapper;
import com.reggie.setmeal.domain.Setmeal;
import com.reggie.setmeal.service.ISetmealService;

/**
 * 套餐Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-02
 */
@Service
public class SetmealServiceImpl implements ISetmealService 
{
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ISetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询套餐
     * 
     * @param id 套餐主键
     * @return 套餐
     */
    @Override
    public Setmeal selectSetmealById(Long id)
    {
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        setmeal.setSetmealDishes(setmealDishService.selectSetmealDishBySetMealId(id));
        Category info = categoryService.getInfo(setmeal.getCategoryId());
        setmeal.setCategoryName(info.getName());
        return setmeal;
    }

    /**
     * 查询套餐列表
     * 
     * @param setmeal 套餐
     * @return 套餐
     */
    @Override
    public List<Setmeal> selectSetmealList(Setmeal setmeal)
    {
        List<Setmeal> setmeals = setmealMapper.selectSetmealList(setmeal);
        for (Setmeal setmeal1 :setmeals) {
            Category info = categoryService.getInfo(setmeal1.getCategoryId());
            setmeal1.setCategoryName(info.getName());
        }
        return setmeals;
    }

    /**
     * 新增套餐
     * 
     * @param setmeal 套餐
     * @return 结果
     */
    @Override
    public int insertSetmeal(Setmeal setmeal)
    {
        int i = 0;
        setmeal.setId(new SnowflakeIdUtils(5, 8).nextId());
        setmeal.setCreateTime(DateUtils.getNowDate());
        setmeal.setUpdateTime(DateUtils.getNowDate());
        setmeal.setCreateUser(SecurityUtils.getUserId());
        setmeal.setUpdateUser(SecurityUtils.getUserId());
        i = setmealMapper.insertSetmeal(setmeal);
        for (SetmealDish setmealDish:setmeal.getSetmealDishes()){
            setmealDish.setSetmealId(setmeal.getId() + "");
            if (setmealDish.getId().equals(setmealDish.getDishId())) {
                setmeal.setId(new SnowflakeIdUtils(5, 8).nextId());
            }
            i += setmealDishService.insertSetmealDish(setmealDish);
        }
        return i;
    }

    /**
     * 修改套餐
     * 
     * @param setmeal 套餐
     * @return 结果
     */
    @Override
    public int updateSetmeal(Setmeal setmeal)
    {
        int i = 0;
        setmeal.setUpdateTime(DateUtils.getNowDate());
        setmeal.setUpdateUser(SecurityUtils.getUserId());
        i = setmealMapper.updateSetmeal(setmeal);


        setmealDishService.updateSetmealDishBySetmeal(setmeal);
        return i;
    }

    /**
     * 批量删除套餐
     * 
     * @param ids 需要删除的套餐主键
     * @return 结果
     */
    @Override
    public int deleteSetmealByIds(Long[] ids)
    {
        return setmealMapper.deleteSetmealByIds(ids);
    }

    /**
     * 删除套餐信息
     * 
     * @param id 套餐主键
     * @return 结果
     */
    @Override
    public int deleteSetmealById(Long id)
    {
        return setmealMapper.deleteSetmealById(id);
    }

    @Override
    public int updateSetmealStatus(Long[] ids, Long status) {
        return setmealMapper.updateSetmealStatus(ids,status);
    }
}
